package com.course.service.impl;

import com.course.dao.UserMapper;
import com.course.pojo.User;
import com.course.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

@Service
public class UserServiceimpl implements UserService{
    private UserMapper usermapper;

    @Override
    public int deleteByPrimaryKey(String username) {
        return usermapper.deleteByPrimaryKey(username);
    }

    @Override
    public int insert(User record) {
        return usermapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return usermapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(String username) {
        return usermapper.selectByPrimaryKey(username);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return usermapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return usermapper.updateByPrimaryKey(record);
    }

    @Override
    public User login(String account, String password) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andPhoneEqualTo(account);
        List<User> list = userMapper.selectByExample(userExample);
        if(list.size() > 0){
            if(list.get(0).getPassword().equals(MD5Util.MD5Encode(password,null))){
                return list.get(0);
            }
            return null;
        }
        return null;
    }

    @Override
    public User register(String account, String password) {
        User user=new User();
        user.setPhone(account);
        user.setIdentity(0);
        user.setPassword(MD5Util.MD5Encode(password,""));
        user.setUserName(account);
        user.setCreateTime(new Date());
        userMapper.insert(user);
        return null;
    }

    @Override
    public boolean is_account(String account) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andPhoneEqualTo(account);
        List<User> list = userMapper.selectByExample(userExample);
        if(list.size()==0){
            return true;
        }
        return false;
    }

    @Override
    public User password_update(int user_id, String pwd, String newPwd) {
        User user = userMapper.selectByPrimaryKey(user_id);
        if(user.getPassword().equals(MD5Util.MD5Encode(pwd, null))){
            user.setPassword(MD5Util.MD5Encode(newPwd, null));
            userMapper.updateByPrimaryKey(user);
            return user;
        }
        return  null;
    }
}
